package com.chance.cc.crawler.development.bootstrap.taobao;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import com.chance.cc.crawler.core.selector.Json;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import groovy.util.logging.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.*;

import static com.chance.cc.crawler.core.script.crawler.CrawlerScript.$_type;

/**
 * @author bx
 * @date 2021/1/8 0008 15:37
 */
@Slf4j
public class TBHttpTest {
    public static Logger logger = LoggerFactory.getLogger(TBHttpTest.class);

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    public static void main(String[] args) {

        HttpConfig httpConfig = HttpConfig.me("test121");
        httpConfig.setProxy(proxy);
        httpConfig.setUseProxy(true);
        httpConfig.setDisableCookie(true);

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());
        List<Thread> threads = new ArrayList<>();
        for (int i=1;i<=1;i++){
            new Thread(new TestForHtmlProductDetailRun(downloader,httpConfig),"thread_"+i).start();
//            threads.add(new Thread(new Test1Run(),"thread_"+i));
//            new Thread(new Test1Run(),"thread_"+i).start();
//            new Thread(new TestForAPPProductDetail1Run(downloader,httpConfig),"thread_"+i).start();
//            new Thread(new TestForProductDetailRun(downloader,httpConfig),"thread_"+i).start();
        }
        for (Thread thread : threads) {
            thread.start();
        }
//        test();
//
//        HttpConfig httpConfig = HttpConfig.me("test");
//        httpConfig.setHttpSite("aaa");
//        httpConfig.setProxy(proxy);
//        httpConfig.setUseProxy(false);
//
//        HttpClientDownloader downloader = new HttpClientDownloader();
//        downloader.setClientGenerator(new HttpClientGenerator());
//        downloader.setProxyProvider(new SiteConfigProxyProvider());
//
//
//
//        String url = "https://h5api.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?jsv=2.6.1&appKey=12574478&t=1623326484650&sign=e3ad586d0e6ad6ca06de23b0fa8ec2ca&api=mtop.taobao.detail.getdetail&v=6.0&isSec=0&ecode=0&AntiFlood=true&AntiCreep=true&ttid=2018%40taobao_h5_9.9.9&H5Request=true&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=%7B%22id%22%3A%22623337888932%22%2C%22hybrid%22%3A%22true%22%2C%22ttid%22%3A%221608030065155%40taobao_android_10.0.0%22%2C%22itemNumId%22%3A%22623337888932%22%2C%22itemId%22%3A%22623337888932%22%2C%22exParams%22%3A%22%7B%5C%22id%5C%22%3A%5C%22623337888932%5C%22%2C%5C%22hybrid%5C%22%3A%5C%22true%5C%22%2C%5C%22ttid%5C%22%3A%5C%221608030065155%40taobao_android_10.0.0%5C%22%7D%22%2C%22detail_v%22%3A%228.0.0%22%2C%22utdid%22%3A%221%22%7D";
//        HttpRequest httpRequest = new HttpRequest();
//        httpRequest.setUrl(url);
//        httpRequest.addHeader("Cookie","cna=krlIGXvjR0ECATohFCJGKHyc");
//        httpRequest.addHeader("Host","h5api.m.taobao.com");
//        httpRequest.addHeader("User-Agent","Mozilla/5.0 (Linux; U; Android 8.0.0; zh-CN; MI 5s Build/OPR1.170623.032) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 UWS/3.22.1.130 Mobile Safari/537.36 AliApp(TB/10.0.0) UCBS/2.11.1.1 TTID/1608030065155@taobao_android_10.0.0 WindVane/8.5.0 1080X1920 UT4Aplus/0.2.16");
//        httpRequest.addHeader("Referer","https://h5.m.taobao.com/");
//
//        HttpPage httpPage = downloader.download(httpRequest, httpConfig);
//        System.out.println(httpConfig);
    }

    static class TestForHtmlProductDetailRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestForHtmlProductDetailRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            List<String> lines = null;
            try {
                lines  = IOUtils.readLines(new FileInputStream("D:\\data\\useragent\\ua.txt"), "utf-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
//            String itemId = "646565408235";
            String urlFormat = "https://h5api.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?jsv=2.6.1&appKey=12574478&" +
                    "t=%s&sign=%s&api=mtop.taobao.detail.getdetail&v=6.0&" +
                    "isSec=0&ecode=0&AntiFlood=true&AntiCreep=true&ttid=2018%%40taobao_h5_9.9.9&H5Request=true&type=jsonp&" +
                    "dataType=jsonp&callback=mtopjsonp1&data=%s";
            List<String> tbProductIds = null;
            try {
                tbProductIds = productIds();
            } catch (IOException e) {
                e.printStackTrace();
            }
            int count =0;
            for (String itemId : tbProductIds) {
//                String paraFoamt = "{\"id\":\"%s\",\"hybrid\":\"true\",\"ttid\":" +
//                        "\"1608030065155@taobao_android_10.0.0\",\"itemNumId\":\"%s\",\"itemId\":\"%s\"," +
//                        "\"exParams\":\"{\\\"id\\\":\\\"%s\\\",\\\"hybrid\\\":\\\"true\\\"," +
//                        "\\\"ttid\\\":\\\"1608030065155@taobao_android_10.0.0\\\"}\",\"detail_v\":\"8.0.0\",\"utdid\":\"1\"}";

                String paraFoamt = "{\"ft\":\"t\",\"isNew\":\"false\",\"downgradesource\":\"h5_2\",\"spm\":\"a230r.1.14.56.2d5b6833WBbUaT\"," +
                        "\"id\":\"%s\",\"ns\":\"1\",\"abbucket\":\"5\",\"bbid\":\"undefined\"," +
                        "\"itemNumId\":\"%s\",\"itemId\":\"%s\",\"exParams\":\"{\\\"ft\\\":\\\"t\\\"," +
                        "\\\"isNew\\\":\\\"false\\\",\\\"downgradesource\\\":\\\"h5_2\\\",\\\"spm\\\":\\\"a230r.1.14.56.2d5b6833WBbUaT\\\"," +
                        "\\\"id\\\":\\\"%s\\\",\\\"ns\\\":\\\"1\\\",\\\"abbucket\\\":\\\"5\\\",\\\"bbid\\\":\\\"undefined\\\"}\"," +
                        "\"detail_v\":\"8.0.0\",\"utdid\":\"1\"}";
                FileOutputStream fileOutputStream = null;
                try {
                    fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\jar\\mub.txt");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                while (true){
                    String ua = lines.get(RandomUtils.nextInt(0, lines.size()));
                    logger.info("ua: "+ ua);
//                    String random = "2079355018106758096";
                    String nubRandom = RandomStringUtils.random(13,"0123456789");
                    String cnaRandom = RandomStringUtils.random(24,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
                    String cookie = "cna="+cnaRandom+";munb="+nubRandom+";unb="+nubRandom;
                    String time = ""+System.currentTimeMillis();
                    String sign = tbHtmlSignature(downloader, itemId, time);
                    HttpRequest httpRequest = new HttpRequest();
                    try {
                        String param = String.format(paraFoamt,itemId,itemId,itemId,itemId);
                        param = URLEncoder.encode(param, "utf-8");
                        String url = String.format(urlFormat,time,sign,param);
                        httpRequest.setUrl(url);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    String str = RandomStringUtils.random(107,"0123456789abcdef");
                    str ="7b22617365727665723b32223a223"+str+"44386d4e62562f502f2f2f2f3842227d";
                    System.out.println(str);

                    httpRequest.addHeader("Host","h5api.m.taobao.com");
//                    httpRequest.addHeader("Cookie","_w_app_lg=0; thw=cn; hng=CN%7Czh-CN%7CCNY%7C156; cna=w5i7FzOwzTgCATohFCIoU9Ne; miid=2079355018106758096; _fbp=fb.1.1621907578910.1489446825; mt=ci%3D-1_1; t=0c740227d36b536a81555051a3742e62; xlly_s=1; _m_h5_tk=6efb49b4924a57bbfdeb397a6d183e29_1623335984831; _m_h5_tk_enc=f3be94bb082ae74e7cfda2e44c7014dd; _samesite_flag_=true; cookie2=16bc68690d0fefb58809aa7fce839d54; ockeqeudmj=tsduuJA%3D; _w_tb_nick=tb6657643383; munb=2210235267133; WAPFDFDTGFG=%2B4cMKKP%2B8PI%2BKK8YQWAlIhqbYGtHM%2Bk%3D; sgcookie=E100ytrZ42BI0uJ8%2FQZKO0JbdwKxCNPRAZYXMjJdBbHBOi%2BS81WU0hZl4BeIH3tDMYgilkbGY32ZiJC5LmXoVHB%2BGw%3D%3D; unb=2210235267133; uc3=nk2=F5RDKmo99uW6Zk0f&id2=UUpgRsYdBlqeWMdlMQ%3D%3D&vt3=F8dCuw7xzFy2ecLV5YU%3D&lg2=WqG3DMC9VAQiUQ%3D%3D; uc1=cookie21=URm48syIZx9a&existShop=false&cookie14=Uoe2zsqS6qmfBg%3D%3D&cookie15=UIHiLt3xD8xYTw%3D%3D; csg=7f29533e; lgc=tb6657643383; ntm=1; cookie17=UUpgRsYdBlqeWMdlMQ%3D%3D; dnk=tb6657643383; skt=95d54b4e10875b2f; uc4=nk4=0%40FY4I6evCZqNMuZs%2BGkItXKs0V2xEwJ4%3D&id4=0%40U2gqyZbkFDHvKCAQB79wRST%2Bz%2BxtIohq; tracknick=tb6657643383; _cc_=UIHiLt3xSw%3D%3D; _l_g_=Ug%3D%3D; sg=33a; _nk_=tb6657643383; cookie1=AiLW1kR5zniOTZKpD5ylv7YeCaGhBbxFC1TzSHYM%2BtI%3D; enc=P9dgL2b0hkAw7Jo7zclm%2F%2FapJDTvJvqkpHjCdYT%2BsH85G8SKPT0AqYUHOZHf%2FkOPeXAKNn7A5upH3dZXJEcT2blTYe9CRvafYOqIQMjkrT8%3D; v=0; l=eBMNc5uejZ2cveC0BOfwnurza77tIIRAguPzaNbMiOCPO0595zVRW6O2tF8pCnGVh6y9R3l-cR2XBeYBqQd-nxvte5DDwQHmn; tfstk=cuCABOV3EuqcOjYR8teu1mOUIMBAZnwvjuxE6ra2ySrSkhGOikCh9DOsZenvwzC..; _tb_token_=eee5b435663b8; isg=BC0t-XzPMU5W7tWi0rXvGEQEPMmnimFc7euNBW8yaEQz5k-YN9tnLUW31LwA5nkU");
                    httpRequest.addHeader("cookie","x5sec=7b22617365727665723b32223a226666653033353032393637373763343330353033373738396566346433336433434b3677726f6747454c584e6b4972456d6f69684b786f504d6a49774f54677a4e5467774e4463304d6a73794d4b4f6875725543227d");
//                   httpRequest.addHeader("cookie",cookie);
                    httpRequest.addHeader("user-Agent",ua);
                    httpRequest.addHeader("Referer","https://h5.m.taobao.com/");


                    Json json = null;
                    HttpPage httpPage = null;
                    try {
                        httpPage = downloader.download(httpRequest, httpConfig);
                        json = new Json(httpPage.getRawText()).removePadding("mtopjsonp1");
                        Configuration conf = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL,Option.SUPPRESS_EXCEPTIONS);
                        json.setConfiguration(conf);
                        String value = json.jsonPath($_type + ".data.apiStack[0].value").get();
                        if (value == null){
                            String trade = json.jsonPath($_type + ".data.trade").get();
                            if (StringUtils.isNoneBlank(trade) && trade.contains("/expired")){
                                logger.warn("tb product {} expired!",itemId);
                            } else {
                                System.out.println("fail munb:"+cookie);
                                try {
                                    IOUtils.writeLines(Arrays.asList("fail munb:"+cookie),"\r\n",fileOutputStream,"utf-8");
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            System.out.println("success munb:"+cookie);
                            try {
                                IOUtils.writeLines(Arrays.asList("success munb:"+cookie),"\r\n",fileOutputStream,"utf-8");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("fail munb:"+cookie);
                        try {
                            IOUtils.writeLines(Arrays.asList("fail munb:"+cookie),"\r\n",fileOutputStream,"utf-8");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

                    System.out.println(httpPage.getStatusCode() + "-->" + httpPage.getRawText());
                }
            }
        }
    }

    static class TestForAPPProductDetailRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestForAPPProductDetailRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }


        @Override
        public void run() {
            HttpRequest httpRequest = new HttpRequest();
//            String deviceId = "Atby4kTGUbEuTBZs4fyq1osda-mNaWHGC0fidYTPFpKG";
//            String sid = "29d2f82b31906126709baffb66d028d3";
//            String ttid = "1608030065155@taobao_android_10.0.0";
//            String uid = "2211470180657";
//            String utdid = "YMCQH9SiYw4DAJ+CTtPBS+EL";
            String deviceId = "AlChtDgjeNqK62mIapWOaPAkN49ZwTldvJQ-3bbjeEB2";
            String sid = "224ab95d09f4ca5a5499fed4c96f2024";
            String ttid = "1568860058617@taobao_android_10.0.0";
            String uid = "2211481958764";
            String utdid = "YLWtjXHbDnsDAPQgIVKHUarl";
//            String itemId = "616123730459";
            List<String> tbProductIds = null;
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream("D:\\chance\\采集\\电商列表页\\20210607\\tb_product_detail_20210602.txt");
                tbProductIds = productIds();
            } catch (IOException e) {
                e.printStackTrace();
            }
            int count =0;
            for (String itemId : tbProductIds) {
                String t = String.valueOf(System.currentTimeMillis()/1000);
                count++;
                String urlPrefix = "https://trade-acs.m.taobao.com/gw/mtop.taobao.detail.getdetail/6.0/?data=";
                String paramFormat = "{\"detail_v\":\"3.3.2\",\"exParams\":{\"ad_type\":\"1.0\",\"appReqFrom\":\"detail\"," +
                        "\"clientCachedTemplateKeys\":\"[{\\\"id\\\":\\\"1538383035450\\\",\\\"version\\\":\\\"138\\\"}]\"," +
                        "\"container_type\":\"xdetail\",\"countryCode\":\"CN\",\"cpuCore\":\"4\",\"cpuMaxHz\":\"1996800\"," +
                        "\"dinamic_v3\":\"true\",\"id\":\""+itemId+"\",\"item_id\":\""+itemId+"\",\"itemid\":\""+itemId+"\"," +
                        "\"latitude\":\"0\",\"locate\":\"guessitem-item\",\"longitude\":\"0\",\"osVersion\":\"26\",\"phoneType\":\"MI+5s\"," +
                        "\"pvid\":\"37933c6c-183b-429f-892b-13ee9d650a7d\",\"rmdChannelCode\":\"guessULike\"," +
                        "\"scm\":\"1007.19223.218505.19675_6547\",\"soVersion\":\"2.0\",\"spm\":\"a2141.1.guessitemtab_1.20\"," +
                        "\"spm-cnt\":\"a2141.7631564\",\"supportV7\":\"true\",\"ultron2\":\"true\",\"utdid\":\""+utdid+"\"}," +
                        "\"itemNumId\":\""+itemId+"\"}";


                String encodeParam = null;
                String  url = null;
                try {
                    encodeParam = URLEncoder.encode(paramFormat,"utf-8").replaceAll("%2B","+");
                    url = urlPrefix + encodeParam;
                    httpRequest.setUrl(url);
                    Map<String, String> signatureMap = tbSignature(downloader, URLDecoder.decode(encodeParam, "UTF-8"), deviceId, sid, t,
                            ttid, uid, utdid,"r_142");
//                    httpRequest.addHeader("x-sid",URLEncoder.encode(sid,"utf-8"));
//                    httpRequest.addHeader("x-uid",URLEncoder.encode(uid,"utf-8"));
                    httpRequest.addHeader("x-pv","6.3");
                    httpRequest.addHeader("x-features","27");
                    httpRequest.addHeader("content-type","application/x-www-form-urlencoded;charset=UTF-8");
                    httpRequest.addHeader("x-t",t);
                    httpRequest.addHeader("x-extdata","openappkey%3DDEFAULT_AUTH");
                    httpRequest.addHeader("x-ttid",URLEncoder.encode(ttid,"utf-8"));
//                    httpRequest.addHeader("x-location","121.589279%2C31.252115");
                    httpRequest.addHeader("x-utdid",URLEncoder.encode(utdid,"utf-8"));
                    httpRequest.addHeader("x-appkey","21646297");
                    httpRequest.addHeader("x-devid",URLEncoder.encode(deviceId,"utf-8"));
                    httpRequest.addHeader("Host","trade-acs.m.taobao.com");
                    httpRequest.getHeaders().putAll(signatureMap);
                    HttpPage httpPage = downloader.download(httpRequest, httpConfig);

                    Json json = new Json(httpPage.getRawText());
                    Configuration conf = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL,Option.SUPPRESS_EXCEPTIONS);
                    json.setConfiguration(conf);
                    String value = json.jsonPath($_type + ".data.apiStack[0].value").get();
                    if (value == null){
                        String trade = json.jsonPath($_type + ".data.trade").get();
                        if (StringUtils.isNoneBlank(trade) && trade.contains("/expired")){
                            logger.warn("tb product {} expired!",itemId);
                        } else {
                            throw new Exception("tb product detail download error");
                        }
                    }

                    System.out.println(httpPage.getStatusCode() + "-->" + httpPage.getRawText());
                    IOUtils.write(httpPage.getRawText()+"\r\n",fileOutputStream, Charset.forName("utf-8"));
                } catch (Exception e) {
                    String value = null;
                    while (value == null){
                        try {
                            Map<String, String> signatureMap = tbSignature(downloader, URLDecoder.decode(encodeParam, "UTF-8"), deviceId, sid, t,
                                    ttid, uid, utdid,"r_142");
                            httpRequest.getHeaders().putAll(signatureMap);
                            HttpPage httpPage = downloader.download(httpRequest, HttpConfig.me("aa"));

                            Json json = new Json(httpPage.getRawText());
                            value = json.jsonPath($_type + ".data.apiStack[0].value").get();
                            System.out.println(httpPage.getStatusCode() + "-->" + httpPage.getRawText());
                            IOUtils.write(httpPage.getRawText()+"\r\n",fileOutputStream, Charset.forName("utf-8"));
                        } catch (Exception ex) {
                            logger.error(ex.getMessage());
                        }
                    }
                }
                logger.info("download product detail success count: {}",count);
            }
        }
    }

    private static List<String> productIds() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\chance\\采集\\电商列表页\\20210607\\tb_product_tmp_20210602.txt"));

        List<String> tbProduct = new ArrayList<>();

//        FileOutputStream tbOutputStream = new FileOutputStream("D:\\chance\\采集\\电商列表页\\20210607\\tb_product_result_20210602.txt",true);

        String line = null;
        while ((line = bufferedReader.readLine()) != null){
            try {
                String[] lineArray = line.split("\t",-1);
                tbProduct.add(lineArray[4].trim());
            } catch (Exception e) {
                System.out.println(line);
                e.printStackTrace();
            }
        }
        return tbProduct;
    }

    static class TestForAPPProductDetail1Run implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestForAPPProductDetail1Run(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            HttpRequest httpRequest = new HttpRequest();

            String  url = "https://trade-acs.m.taobao.com/gw/mtop.taobao.detail.getdetail/6.0/?data=%7B%22detail_v%22%3A%223.3.2%22%2C%22exParams%22%3A%22%7B%5C%22%5C%22%3A%5C%22%5C%22%2C%5C%22ad_type%5C%22%3A%5C%221.0%5C%22%2C%5C%22appReqFrom%5C%22%3A%5C%22detail%5C%22%2C%5C%22clientCachedTemplateKeys%5C%22%3A%5C%22%5B%7B%5C%5C%5C%22id%5C%5C%5C%22%3A%5C%5C%5C%221538383035450%5C%5C%5C%22%2C%5C%5C%5C%22version%5C%5C%5C%22%3A%5C%5C%5C%22138%5C%5C%5C%22%7D%5D%5C%22%2C%5C%22container_type%5C%22%3A%5C%22xdetail%5C%22%2C%5C%22countryCode%5C%22%3A%5C%22CN%5C%22%2C%5C%22cpuCore%5C%22%3A%5C%224%5C%22%2C%5C%22cpuMaxHz%5C%22%3A%5C%221996800%5C%22%2C%5C%22detailAlgoParam%5C%22%3A%5C%22%25E5%25A5%25B3%25E5%2586%25AC%25E5%25A4%2596%25E5%25A5%2597%5C%22%2C%5C%22dinamic_v3%5C%22%3A%5C%22true%5C%22%2C%5C%22from%5C%22%3A%5C%22search%5C%22%2C%5C%22id%5C%22%3A%5C%22633251513280%5C%22%2C%5C%22itemId%5C%22%3A%5C%22633251513280%5C%22%2C%5C%22item_id%5C%22%3A%5C%22633251513280%5C%22%2C%5C%22latitude%5C%22%3A%5C%2231.252115%5C%22%2C%5C%22list_type%5C%22%3A%5C%22search%5C%22%2C%5C%22locType%5C%22%3A%5C%22%5C%22%2C%5C%22longitude%5C%22%3A%5C%22121.589279%5C%22%2C%5C%22nick%5C%22%3A%5C%22tb288552944%5C%22%2C%5C%22osVersion%5C%22%3A%5C%2226%5C%22%2C%5C%22phoneType%5C%22%3A%5C%22MI+5s%5C%22%2C%5C%22skuId%5C%22%3A%5C%22%5C%22%2C%5C%22soVersion%5C%22%3A%5C%222.0%5C%22%2C%5C%22spm%5C%22%3A%5C%22a2141.7631557.itemlist.9%5C%22%2C%5C%22spm-cnt%5C%22%3A%5C%22a2141.7631564%5C%22%2C%5C%22store_id%5C%22%3A%5C%22%5C%22%2C%5C%22supportV7%5C%22%3A%5C%22true%5C%22%2C%5C%22ultron2%5C%22%3A%5C%22true%5C%22%2C%5C%22utdid%5C%22%3A%5C%22YLWtjXHbDnsDAPQgIVKHUarl%5C%22%7D%22%2C%22itemNumId%22%3A%22633251513280%22%7D";
            httpRequest.setUrl(url);
            httpRequest.addHeader("x-sgext","JAJqrae4WCg%2FVHe23wb7wa1bnVuYWI5YnViaU45bmliOW4FbgUeZRpxTgVqBWoFagVqBXYFbgViZRpxbjlubWJ5anl6YUpxJnQzLDphZzFmUUpUOzgyUXo5bnluOWJ9JywyOWp1anUmdSZxJn0mcSZ9JnkmcSZxJnFmOXo5ezEnJSZxJn0mdSZ1JnUmt");
            httpRequest.addHeader("x-sign","azYBCM003xAALeEKAXclnXu3UApLzeEN6ec6W%2BaTLiSxAhELuplSzqxxoSp9veGELNul34RKEo8yt9VJQTyluUBJR5uhXeEN4%2F3hDe");
            httpRequest.addHeader("x-sid","224ab95d09f4ca5a5499fed4c96f2024");
            httpRequest.addHeader("x-uid","2211481958764");
            httpRequest.addHeader("x-pv","6.3");
            httpRequest.addHeader("x-features","27");
            httpRequest.addHeader("x-mini-wua","HHnB_WP7yEKwqzxGcJ2%2F9kWbSE6n5IfYp%2FK%2FDF%2Fp5pbAXy7OUguEm6JMg%2BJ7lbC3%2B6xOVPyvbrk0AXGJj24B8Z6HOXtCLkBSxTUxoYclj5gFEE2e62aA6zmRXBAszY%2ByslI%2FTgsnJMA2fkEbOjTfuZ91YzAyeAqX0BAGKUJ9tyya0zGk%3D");
            httpRequest.addHeader("content-type","application/x-www-form-urlencoded;charset=UTF-8");
            httpRequest.addHeader("x-t","1623118764");
            httpRequest.addHeader("x-extdata","openappkey%3DDEFAULT_AUTH");
            httpRequest.addHeader("x-ttid","1568860058617%40taobao_android_10.0.0");
            httpRequest.addHeader("x-location","121.589279%2C31.252115");
            httpRequest.addHeader("x-umt","8fAAWRhLPKSHVgJ55DyAzsOhZoAxZzUG");
            httpRequest.addHeader("x-utdid","YLWtjXHbDnsDAPQgIVKHUarl");
            httpRequest.addHeader("x-appkey","21646297");
            httpRequest.addHeader("x-devid","AlChtDgjeNqK62mIapWOaPAkN49ZwTldvJQ-3bbjeEB2");
            httpRequest.addHeader("Host","trade-acs.m.taobao.com");
            HttpPage httpPage = downloader.download(httpRequest, HttpConfig.me("aa"));

            System.out.println(httpPage.getStatusCode() + "-->" + httpPage.getRawText());

        }
    }

//    private static Map<String,String> signature(String data,String deviceId,String sid,String t,
//                                                String ttid,String uid, String utdid){
//        Map<String,String> dataMap = new HashMap<>();
//        dataMap.put("api","mtop.taobao.detail.getdetail");
//        dataMap.put("appKey","21646297");
//        dataMap.put("data",data);
//        dataMap.put("deviceId",deviceId);
//        dataMap.put("extdata","openappkey=DEFAULT_AUTH");
//        dataMap.put("lat","31.25238");
//        dataMap.put("lng","121.589016");
//        dataMap.put("sid",sid);
//        dataMap.put("t",t);
//        dataMap.put("ttid",ttid);
//        dataMap.put("uid",uid);
//        dataMap.put("utdid",utdid);
//        dataMap.put("v","6.0");
//        dataMap.put("x-features","27");
//        Map<String,String> pageMap = new HashMap<>();
//        pageMap.put("pageId","http://item.taobao.com/item.htm");
//        pageMap.put("pageName","com.taobao.android.detail.wrapper.activity.DetailActivity");
//
//    }

    private static Map<String,String> tbSignature(HttpClientDownloader downloader,String data,String deviceId,String sid,String t,
                                                  String ttid,String uid, String utdid,String str3){

        Map<String,String> dataMap = new HashMap<>();
        dataMap.put("api","mtop.taobao.detail.getdetail");
        dataMap.put("appKey","21646297");
        dataMap.put("data",data);
        dataMap.put("deviceId",deviceId);
        dataMap.put("extdata","openappkey=DEFAULT_AUTH");
//        dataMap.put("lat","31.252115");
//        dataMap.put("lng","121.589279");
//        dataMap.put("sid",sid);
        dataMap.put("t",t);
        dataMap.put("ttid",ttid);
//        dataMap.put("uid",uid);
        dataMap.put("utdid",utdid);
        dataMap.put("v","6.0");
        dataMap.put("x-features","27");

        Map<String,String> pageMap = new HashMap<>();
        pageMap.put("pageId","http://item.taobao.com/item.htm");
        pageMap.put("pageName","com.taobao.android.detail.wrapper.activity.DetailActivity");

        String signatureUrl = "http://192.168.1.214:8384/frida/tb/signature";
        HttpConfig tbSignatureConfig = HttpConfig.me("test2");
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl(signatureUrl);
        Map<String,Object> tbDataMap = new HashMap<>();
        tbDataMap.put("map1",JSON.toJSONString(dataMap));
        tbDataMap.put("map2",JSON.toJSONString(pageMap));
        tbDataMap.put("str1",21646297);
        tbDataMap.put("str2","null");
        tbDataMap.put("b1",false);
        tbDataMap.put("str3",str3);
        httpRequest.setRequestBody(HttpRequestBody.form(tbDataMap,"utf-8"));
        httpRequest.setMethod("POST");
        HttpPage download = downloader.download(httpRequest, tbSignatureConfig);
        String rawText = download.getRawText();
        Map signatureMap = JSON.parseObject(rawText, Map.class);
        Map<String,String> encodeSignatureMap = new HashMap<>();
        for (Object objKey : signatureMap.keySet()) {
            String key = objKey.toString();
//            if ("x-umt".equals(key)){
//                continue;
//            }
            String value = signatureMap.get(key).toString();
            try {
                value = URLEncoder.encode(value,"utf-8");
                encodeSignatureMap.put(key,value);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return encodeSignatureMap;
    }
    private static String tbHtmlSignature(HttpClientDownloader downloader,String itemId,String time){

        String signatureUrl = "http://192.168.1.214:8899/encrypt/taobao?itemId="+itemId+"&time="+time;
        HttpConfig tbSignatureConfig = HttpConfig.me("test2");
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl(signatureUrl);
        HttpPage download = downloader.download(httpRequest, tbSignatureConfig);
        return download.getRawText();
    }

    private static void test(){
        Map<String,Map<String,String>> map2 = new HashMap<>();
        Map<String,String> map3 = new HashMap<>();
        map3.put("b","b");
        map2.put("a",map3);
        System.out.println(map2.get("a"));
//        System.out.println(map2.get("a"));
        map3 = new HashMap<>();
        map3.put("c","c");
        System.out.println(map2.get("a"));

    }

    static class Test1Run implements Runnable{

        @Override
        public void run() {
            test test = new test();
            logger.info(test.a());

        }
    }


}

class test{
    private static Object obj = new Object();
    private static String aa = "aaa";
    public String a(){
        if (aa.equals("aaa")){
//            synchronized (obj) {
//                if (aa.equals("aaa")){
//                    System.out.println(Thread.currentThread()+ "-->" + aa);;
//                    aa = "bbb";
//                }
//            }
            System.out.println(Thread.currentThread()+ "-->" + aa);;
            aa = "bbb";
        }
        return aa;
    }
}
